#pragma once
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stdarg.h>
#include <time.h>

// 日志级别
#define DEBUG 0
#define NORMAL 1
#define WARNING 2
#define ERROR 3
#define DATAL 4
#define FATAL 5

const char *gLevelMap[] = {
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

#define LOGFILE "./threadpool.log"

void logMessage(int level, const char* format, ...)
{
#ifndef DEBUG_SHOW
    if(level==DEBUG) return;
#endif
    char stdBuffer[1024]; //标准部分
    time_t timestamp=time(nullptr);
    //struct tm *localtime = localtime(&timestamp);
    //snprintf(stdBuffer, sizeof(stdBuffer),"[%s] [%ld]", gLevelMap[level],timestamp);
    snprintf(stdBuffer, sizeof stdBuffer, "[%s] [%ld] ", gLevelMap[level], timestamp);

    char logBuffer[1024];  //自定义部分
    va_list args;
    va_start(args,format);
    //vprintf(format,args);  //网显示器打印
    vsnprintf(logBuffer,sizeof logBuffer,format,args);  //往字符串打
    va_end(args);

    // FILE* fp=fopen(LOGFILE,"a");  // 往文件打印

    printf("%s %s\n",stdBuffer,logBuffer);  // 打印再显示器上
    // fprintf(fp,"%s %s\n",stdBuffer,logBuffer);// 往文件打印
    // fclose(fp);
}